//
// Created by 11510 on 2025/10/2.
//
#pragma once
#include "CommandContext.h"
#include "cxxopts.hpp"
#include <string>
#include <optional>
#include <future>
namespace xclib_client {
    static cxxopts::ParseResult parseOptions(cxxopts::Options &options, const std::vector<std::string> &args) {
        // cxxopts only exposes parse(int argc, const char* const* argv) in the headers.
        // Convert vector<string> into an argv-like array of const char* for parsing.
        std::vector<const char *> argv;
        argv.reserve(args.size());
        for (const auto &s: args)
            argv.push_back(s.c_str());
        return options.parse(static_cast<int>(argv.size()), argv.data());
    }

    class ICommand {
    public:
        ICommand(const std::string &name, CommandContext ctx) : name(name), ctx(ctx) {}

        virtual ~ICommand() = default;

        const std::string &getName() const { return name; }

        // Run the command. 'wait' indicates whether to run synchronously.
        // Returns an optional future if run asynchronously.
        virtual std::optional<std::future<int>> run(const std::vector<std::string> &args, bool wait) = 0;

        // Short help for 'help' listing
        virtual std::string shortHelp() const = 0;

        // Long man page for 'man <command>'
        virtual std::string longHelp() const = 0;

    protected:
        std::string name;
        CommandContext ctx;
    };
}